package com.tickdroid;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Edit extends Activity {
	
	
	private ListView theList;
	private LinearLayout buttonLayout;
	private Button btnAdd, btnDelete, btnRename;
	private EditText text;
	private int counter;
	

	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.makelist);
        
        initializer();
        
      //  Bundle listToEdit = getIntent().getExtras();
      //  Log.v("HER",listToEdit.getString("com.tickdroid.listname") );
        
        
        final ArrayList<String> list = new ArrayList<String>();
        final ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,	android.R.layout.simple_list_item_multiple_choice, list);
        theList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
			/*	AlertDialog.Builder adb=new AlertDialog.Builder(Edit.this);
				adb.setTitle("LVSelectedItemExample");
				adb.setMessage("Selected Item is = "+theList.getItemAtPosition(position));
				adb.setPositiveButton("Ok", null);
				adb.show();
				
			*/

		    	//hvor mange elementer der er valgt
				counter=0;
		    	
		    	for(int i = 0; i < list.size(); i++){
		    		if(theList.getCheckedItemPositions().get(i)) 
		    			counter ++; 
		    		}
		    	showButtons(counter);
			}
        	
        	
        });
        
    	theList.setAdapter(adapt);
		theList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		
		
        btnAdd.setOnClickListener(new OnClickListener() {
  		   
		    public void onClick(View v) {
		    	  	
		    	list.add(text.getText().toString());
		    	adapt.notifyDataSetChanged();
		    	text.setText("");
				theList.setSelection(list.size()); 

		    	
		    }
        });
        
        btnDelete.setOnClickListener(new OnClickListener() {
   		   
		    public void onClick(View v) {
		    	  	
		    	list.remove(0);
		    	adapt.notifyDataSetChanged();
		    	
		    }
        });
        
        btnRename.setOnClickListener(new OnClickListener() {
   		   
		    public void onClick(View v) {
		    	  	
		    	//get selected item
		    	//change name in the ArrayList

		    	
		    }
        });

     

    
       
       
        
       
	}
	
	private void showButtons(int which){
		
		if(which == 1){
            btnDelete.setVisibility(View.VISIBLE);
            btnRename.setVisibility(View.VISIBLE);
            btnAdd.setVisibility(View.GONE);
            text.setVisibility(View.GONE);
    	}
    	else if(which == 0){
            btnDelete.setVisibility(View.GONE);
            btnRename.setVisibility(View.GONE);
            btnAdd.setVisibility(View.VISIBLE);
            text.setVisibility(View.VISIBLE);
    	}
    	else if(which > 1){
            btnDelete.setVisibility(View.VISIBLE);
            btnRename.setVisibility(View.GONE);
            btnAdd.setVisibility(View.GONE);
            text.setVisibility(View.GONE);
    	}
		
	}
	
	
	
	private void initializer(){
		
        theList = (ListView) findViewById(R.id.lstvTheList);
        buttonLayout = (LinearLayout) findViewById(R.id.llButtonSpace);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnRename = (Button) findViewById(R.id.btnRename);
		btnAdd = (Button) findViewById(R.id.btnAdd);
        text = (EditText) findViewById(R.id.txtSubject);
        
        btnDelete.setVisibility(View.GONE);
		btnRename.setVisibility(View.GONE);
        //btnAdd.setVisibility(View.GONE);
        //text.setVisibility(View.GONE);
	}
	

}